package com.danny.Controller;

import com.danny.mapper.UserMapper;
import com.danny.pojo.User;
import com.danny.pojo.UserLoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;


    /**
     * @param  sessionId     保存在服務器中---15分鍾
     * @return true or false
     * @description 驗證是否登錄`
     */
    @GetMapping("checkLogin")
    public boolean checkLogin(String sessionId, HttpServletRequest request){
        return request.getSession().getId().equals(sessionId);
    }

    /**
     * @param userName 用戶名
     * @param passWord 密碼
     * @param request  http請求
     * @return
     */
    @GetMapping("/login")
    public UserLoginInfo login(String userName, String passWord, HttpServletRequest request){
        HashMap<String, Object> map = new HashMap<>();
        map.put("userName",userName);
        map.put("page",0);
        map.put("pageSize",9);

        List<User> admin =userMapper.queryUser(map);


        User user = admin.get(0);

        if(user.getUserName().equals(userName) && user.getPassWord().equals(passWord)){
            request.getSession(true).setAttribute("userName",userName);
            return new UserLoginInfo("登錄成功",request.getSession(true).getId());
        }

        return new UserLoginInfo("error","");
    }

    /**
     *
     * @description 查詢所有用戶...
     * @return      List
     */
    @GetMapping("/user")
    public List<User> user(){

        return userMapper.queryAllUser();
    }

    //這里是模糊查詢......
    //有參...
    @GetMapping("/search")
    public List<User>  getUser(String userName,int page,int pageSize){
        HashMap<String, Object> map = new HashMap<>();
        map.put("userName",userName);
        map.put("page",0);
        map.put("pageSize",9);
        return userMapper.queryUser(map);
    }

    //模糊查詢的個數..

    @GetMapping("/searchCount")
    public int getUserCount(String userName){
        return userMapper.queryUserCount(userName);
    }

    //堵加用戶...
    @GetMapping("/addUser")
    public String addUser(User user){
        if(userMapper.addUser(user)!=0){
            return "inserted";
        }
        return "error";

    }
    //updateUserById....
    @GetMapping("/updateUser")
    public String updateUser(User user){
        if(userMapper.updateUser(user)!=0){
            return "updated";
        }
        return "error";
    }


    @GetMapping("/deleteUser")
    public String deleteUser(int userId){
        if(userMapper.deleteUserById(userId)!=0){
            return "deleted";
        }
        return "error";
    }

    /**
     * 無參數的分頁...
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/queryLimit")
    public List<User> queryLimit(int page,int pageSize){
        HashMap<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("pageSize",pageSize);

        return userMapper.queryLimit(map);
    }

    @GetMapping("/queryLimitHasInput")
    public List<User> queryLimitHasInput(String userName,int page,int pageSize){
        HashMap<String, Object> map = new HashMap<>();
        map.put("userName",userName);
        map.put("page",page);
        map.put("pageSize",pageSize);

        return userMapper.queryLimitHasInput(map);
    }

    @GetMapping("/pageCount")
    public int pageCount(){
        return userMapper.pageCount();
    }

}